package javamusic.Servlet.Ventas;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import javamusic.excepcion.DAOExcepcion;
import javamusic.modelo.DetalleVenta;
import javamusic.negocio.GestionVenta;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletVentasBuscar
 */
@WebServlet("/ServletVentasBuscar")
public class ServletVentasBuscar extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ServletVentasBuscar() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String  local = request.getParameter("local");	
		String  fecInicio = request.getParameter("fecInicio");
		String  fecFin = request.getParameter("fecFin");
		
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy",new Locale("es","ES"));
		
		Integer idLocal = 0;
		
		if(local != null && local != "") idLocal = Integer.parseInt(local);
		
		GestionVenta negocio = new GestionVenta();  
		
		try {
						
			Date fechaInicio = format.parse(fecInicio);
			Date fechaFin = format.parse(fecFin);
			Collection<DetalleVenta> lstDetalleVenta = negocio.ListarDetalleVenta(idLocal, new java.sql.Date(fechaInicio.getTime()),  new java.sql.Date(fechaFin.getTime()));
			
			request.setAttribute("DetalleVenta_Result", lstDetalleVenta);
			RequestDispatcher rd = request.getRequestDispatcher("frmVentas.jsp");		
			rd.forward(request, response);
		} catch (DAOExcepcion | ParseException e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}
		

}
